import LGXCOM from '../common/common.js'
import baseunitgrah from './baseunitgrah.js'
import { CoordTRFUtil } from '../common/tools/coordtool.js'
import Coord from '../common/struct/coord.js'
import lnActPkg from './action/lineaction.js'
import UtilFuncs from '../common/tools/utilfuncs.js'
var funcs = UtilFuncs.UtilFuncs;

import globalConst from '../common/globalConst.js'
var global = globalConst.GlobalConst

import PolylinePkg from '../common/struct/basestruct/polyline.js'
var Polyline = PolylinePkg.Polyline;

import {LGXEnums}  from '../common/sysenum.js'

import arrowutil from './paintutil/arrowutil.js'
import {Strokeutil} from '../graph/paintutil/strokeutil.js'

import { GlobalVar } from '../common/globalvar.js'
import { CrossModLineSvc } from '../service/crossmodlinesvc.js'
import { GlobalConstObj } from '../common/globalconstobj.js'

import absgraph from './absgraph.js'

const anotationLineColor = '#00ff00';
const anotationLabColor = '#ffffff';
const ST_featurePT_IDX = 2;//1留给整体移动

class LineGraph extends baseunitgrah.BaseUnitGraph {
    constructor(type) {
        super(type);
        this.m_graphType = LGXEnums.LGXGraphType.LineGraph_type;
        this.setPos(new Coord.MultiPos());
        this.setActionSvc(new lnActPkg.LineGraphAction(this));
        
        this.close = false;
        this.visionType = GlobalVar.lineVisionType;//线路正交编辑，自动横平竖直
        this.symbolLeadwireFlag = false;
    }

    setClose(v) { this.close = v; }
    getClose() { return this.close; }
    setSymbolLeadwireFlag(v) { this.symbolLeadwireFlag = v; }
    getSymbolLeadwireFlag() { return this.symbolLeadwireFlag; }

    display(map, mapInfo,forClip) {
        if (this.visionType == LGXEnums.lineVisionType.curve) {
            this.disBezierCurve(map, mapInfo);
            return;
        }
        var position = this.getPos();
        var ptCount = position.posList.length;
        if (ptCount < 2) {
            this.setUseful(0);
            return;
        }
        map.save();
        map.beginPath();
        var ptlst = new Array();
        var pt = position.posList[0];
        var p = CoordTRFUtil.world2Screen(pt, mapInfo);
        var p0 = p;
        map.moveTo(p.x, p.y);
        ptlst.push(p);
        for (var i = 1; i < ptCount; i++) {
            pt = position.posList[i];
            if (isNaN(pt.x) || isNaN(pt.y)) {
                if (!this._modifing)
                    this.setUseful(0);
                return;
            }
            p = CoordTRFUtil.world2Screen(pt, mapInfo);
            map.lineTo(p.x, p.y);
            ptlst.push(p);
        }
        if (this.close) {
            map.lineTo(p0.x, p0.y);
        }
        Strokeutil.active(this._lineDef, this, map, mapInfo)
        // map.closePath();
        if (!this.close) {
            if (this._lineDef.stArrowType != "none") {
                let p1 = position.posList[0];
                let p2 = position.posList[1];
                let vector = funcs.getVector(p1, p2);
                arrowutil.ArrowUtil.dis(p1, vector, map, mapInfo, this._lineDef.color.toHexString(), this._lineDef.stArrowType);
            }

            if (this._lineDef.endArrowType != "none") {
                let p1 = position.posList[ptCount - 1];
                let p2 = position.posList[ptCount - 2];
                let vector = funcs.getVector(p1, p2);
                arrowutil.ArrowUtil.dis(p1, vector, map, mapInfo, this._lineDef.color.toHexString(), this._lineDef.endArrowType);
            }
        }

        if (this._finishFlag == 0 && this._startFlag == 1) {
            //正在绘制中的线路，显示当前线段长度标识
            this.displayLenthText(position.posList, map, mapInfo);
        }

        super.displayGraphScope(map, mapInfo);
        this.displayFeaturePoint(map, ptlst, mapInfo);
        this.displayConnNode(map, mapInfo);
        map.restore();
    }

    displayConnNode(map, mapInfo) {
        let disflag = false;
        if (mapInfo._disScope && this.m_topoRes) {
            disflag = true;
        }
        else if (this.m_topoRes && this.getFinish() == 0 && this.getUseful() == 1) {
            disflag = true;
        }

        //if (disflag) 
        if (this.m_topoRes)
        {
            let nodelist = this.m_topoRes.m_connNodeList;
            for (let i = 0; i < nodelist.length; i++) {
                let node = nodelist[i];
                let nodePt = node.getPos();
                this.displayCircleMark(nodePt, map, mapInfo);
            }
        }
    }

    displayCircleMark(nodePt, map, mapInfo) {
        let sp = CoordTRFUtil.world2Screen(nodePt, mapInfo);
        map.beginPath();
        map.ellipse(sp.x, sp.y, 5, 5, 0, 0, Math.PI * 2, false);
        map.closePath();
        map.fillStyle = "#ffff00";
        map.fill();
    }

    displayLenthText(posList, map, mapInfo) {
        const anotationOffLen = 20;
        const gap = anotationOffLen / mapInfo.getLenthDisScale();
        let poscount = posList.length;
        let p1 = posList[poscount - 2];
        let p2 = posList[poscount - 1];
        let lineLenth = funcs.distancePoint(p1, p2, 1);

        let vector1 = funcs.getVector(p1, p2);
        let rotVct1 = vector1.rotateCopy(90);
        let x10 = p1.x + rotVct1.m * gap * 0.5;
        let y10 = p1.y + rotVct1.n * gap * 0.5;
        let p10 = { x: x10, y: y10 };

        let x11 = p1.x + rotVct1.m * gap * 2;
        let y11 = p1.y + rotVct1.n * gap * 2;
        let p11 = { x: x11, y: y11 };

        let midx1 = (x10 + x11) / 2;
        let midy1 = (y10 + y11) / 2;
        let midpt1 = { x: midx1, y: midy1 };

        arrowutil.ArrowUtil.dis(midpt1, vector1, map, mapInfo, anotationLineColor, true, true);

        let vector2 = funcs.getVector(p2, p1);
        let rotVct2 = vector2.rotateCopy(-90);
        let x20 = p2.x + rotVct2.m * gap * 0.5;
        let y20 = p2.y + rotVct2.n * gap * 0.5;
        let p20 = { x: x20, y: y20 };

        let x21 = p2.x + rotVct2.m * gap * 2;
        let y21 = p2.y + rotVct2.n * gap * 2;
        let p21 = { x: x21, y: y21 };

        let midx2 = (x20 + x21) / 2;
        let midy2 = (y20 + y21) / 2;
        let midpt2 = { x: midx2, y: midy2 };

        arrowutil.ArrowUtil.dis(midpt2, vector2, map, mapInfo, anotationLineColor, true, true);

        this.displayLineSec(p10, p11, map, mapInfo);
        this.displayLineSec(p20, p21, map, mapInfo);

        let dashLen = lineLenth / 3;
        let x12 = midpt1.x + vector1.m * dashLen;
        let y12 = midpt1.y + vector1.n * dashLen;
        let p12 = { x: x12, y: y12 };

        this.displayLineSec(midpt1, p12, map, mapInfo);

        let x22 = midpt2.x + vector2.m * dashLen;
        let y22 = midpt2.y + vector2.n * dashLen;
        let p22 = { x: x22, y: y22 };

        this.displayLineSec(midpt2, p22, map, mapInfo);

        let sp1 = CoordTRFUtil.world2Screen(midpt1, mapInfo);
        let sp2 = CoordTRFUtil.world2Screen(midpt2, mapInfo);

        let tx = (sp1.x + sp2.x) / 2;
        let ty = (sp1.y + sp2.y) / 2;
        let lineAngle = vector1.angleFrmXAxisPositiveDir();
        let rotv = funcs.degree2Radian(lineAngle);

        map.save();
        map.translate(tx, ty);
        map.rotate(-rotv);
        map.translate(-tx, -ty);

        let text = lineLenth.toFixed(2);
        map.fillStyle = anotationLabColor;
        var canvasfont = "18px 仿宋";
        map.font = canvasfont;
        map.textAlign = "center";

        var metrics = map.measureText(text);
        var lineHeight = metrics.fontBoundingBoxAscent;
        let width = metrics.width;
        map.fillText(text, tx, ty);
        map.restore();
    }

    displayLineSec(p1, p2, map, mapInfo) {
        map.beginPath();
        map.lineWidth = 1;
        map.strokeStyle = anotationLineColor;

        var sp1 = CoordTRFUtil.world2Screen(p1, mapInfo);
        map.moveTo(sp1.x, sp1.y);

        var sp2 = CoordTRFUtil.world2Screen(p2, mapInfo);
        map.lineTo(sp2.x, sp2.y);

        map.stroke();
        map.closePath();
    }

    disInSymbol(transformInfo,map, mapInfo,forClip) {
        if (this.symbolLeadwireFlag) {
            return;
        }
        var position = this.getPosCopy();
        var lenth = position.posList.length;
        if (lenth < 2){
            return ;
        }
        
        let tt = new absgraph.LineUnitCounter();
        tt.lineUnitCount++;
        
        let sptLst =  [];
        for (var i = 0; i < lenth; i++) {
            let pt = position.posList[i];
            let wpt = CoordTRFUtil.transLocalPt2World(pt,0,transformInfo);
            let spt = CoordTRFUtil.world2Screen(wpt,mapInfo);
            sptLst.push(spt);
        }
        if (this.close){
            sptLst.push(sptLst[0]);
        }

        map.beginPath();
        let spt = sptLst[0];
        map.moveTo(spt.x, spt.y);
        for (var i = 1; i < lenth; i++) {
            spt = sptLst[i];
            map.lineTo(spt.x, spt.y);
        }
        Strokeutil.active(this._lineDef, this, map, mapInfo);
    }

    calScope(mapscale) {
        // console.log("line graph calculate scope");
        var position = this.getPos();
        var lenth = position.posList.length;
        if (lenth < 2) {
            return;
        }
        var rect = position.getZone();
        let invalid = false;
        if (this.getFinish() == 1 && rect.width() < global.ZERO_4E && rect.height() < global.ZERO_4E) {
            invalid = true;
            this.setUseful(0);
        }
        this._zone.clone(rect);
    }

    getLocalZone() {
        return super.getMultiPointGraphLocalZone();
    }

    distance(tPos, scaling, mapscale) {
        var position = this.getPosCopy();
        var lenth = position.posList.length;
        if (lenth < 2) {
            return -1;
        }
        // if (!this.preCheckInScope(tPos, scaling)) {
        //     return -1;
        // }

        var cmpGap = super.getSelCmpGap(scaling, mapscale);
        var minDis = -1;
        var pCntPos = null;
        var pPrePos = position.posList[0];
        for (var i = 0; i < lenth - 1; i++) {
            pCntPos = position.posList[i + 1];
            var dis = funcs.distanceLineSec4Visual(tPos, pPrePos, pCntPos, scaling);
            if (dis >= 0 && ((dis < minDis) || (minDis < 0))) {
                minDis = dis;
            }
            pPrePos = pCntPos;
        }
        if (minDis <= cmpGap && minDis >= 0) {
            return minDis;
        }
        else {
            return -1;
        }
    }

    preCheckInScope(mousePt, scaling) {
        let tempRect = new LGXCOM.LGXRect();
        tempRect.clone(this._zone);

        const cmpGap = 10;
        tempRect.expand(cmpGap / scaling);
        let flag = funcs.chkPtInRect(mousePt, tempRect);
        return flag;
    }

    displayFeaturePoint(map, ptLst, mapInfo) {
        if (!this._selected) {
            return;
        }
        let lenth = ptLst.length;
        for (let i = 0; i < lenth; i++) {
            let pt = ptLst[i];
            super.drawFocusPoint(map, pt.x, pt.y, "#0000ff");
        }

        //显示中点
        let prePt = ptLst[0];
        if (this.chkClose()) {
            ptLst.push(prePt);
        }
        let colorstr = GlobalVar.highlightColor.toHexString();
        lenth = ptLst.length;
        for (let i = 1; i < lenth; i++) {
            let nextPt = ptLst[i];
            let x = (prePt.x + nextPt.x) / 2;
            let y = (prePt.y + nextPt.y) / 2;
            super.drawFocusPoint(map, x, y, colorstr);
            prePt = nextPt;
        }
    }

    calculateSelKind(tPoint, scaling, mapscale) {
        if (!this.preCheckInScope(tPoint, scaling)) {
            return -1;
        }
        // console.log("line calculateSelKind");
        var position = this.getPosCopy();
        var lenth = position.posList.length;
        if (lenth < 2) {
            return 0;
        }
        var seleKind = 0;
        var tAbsorbPt = new LGXCOM.LGXPoint();
        tAbsorbPt.clone(tPoint);

        var cmpGap = super.getSelCmpGap(scaling, mapscale);
        var minDis = -1;

        if (this.symbolLeadwireFlag && this.m_pContainer) {
            let tailPt = position.getTail();
            let dis = funcs.distancePoint(tPoint, tailPt, scaling);
            if (dis <= cmpGap && dis >= 0) {
                tAbsorbPt.clone(tailPt);
                seleKind = ST_featurePT_IDX + lenth - 1;
            }
            else {
                seleKind = 1;
            }
        }
        else {
            let pCntPos = null;
            for (var i = 0; i < lenth; i++) {
                pCntPos = position.posList[i];
                let dis = funcs.distancePoint(tPoint, pCntPos, scaling);
                if (minDis < 0 && dis >= 0) {
                    minDis = dis;
                }
                if (dis <= cmpGap && dis >= 0 && dis <= minDis) {
                    minDis = dis;
                    tAbsorbPt.clone(tPoint);
                    seleKind = ST_featurePT_IDX + i;
                }
            }

            let pPrePos = position.posList[0];
            if (this.chkClose()) {
                position.addPoint(pPrePos);
            }

            //是否拾取了中点
            {
                for (var i = 1; i < lenth; i++) {
                    pCntPos = position.posList[i];
                    let midPt = funcs.getMidPoint(pPrePos, pCntPos);
                    let dis = funcs.distancePoint(tPoint, midPt, scaling);
                    if (minDis < 0 && dis >= 0) {
                        minDis = dis;
                    }
                    if (dis <= cmpGap && dis >= 0 && dis <= minDis) {
                        minDis = dis;
                        tAbsorbPt.clone(midPt);
                        seleKind = lenth + ST_featurePT_IDX + i - 1;
                    }
                    pPrePos = pCntPos;
                }
            }

            if (seleKind == 0) {
                pPrePos = position.posList[0];
                for (let i = 1; i < lenth; i++) {
                    pCntPos = position.posList[i];
                    let dis = funcs.distanceLineSec4Visual(tPoint, pPrePos, pCntPos, scaling);
                    if (minDis < 0 && dis >= 0) {
                        minDis = dis;
                    }
                    if (dis <= cmpGap && dis >= 0 && dis <= minDis) {
                        let nConnType = funcs.adsorbPosOnLineSec(tAbsorbPt, pPrePos, pCntPos, scaling);
                        if (nConnType % 10 == 0) {
                            seleKind = 1;//选在线上，非特征点，整体移动
                            minDis = dis;
                            tAbsorbPt.clone(tPoint);
                        }
                    }
                    pPrePos = pCntPos;
                }
            }
        }

        if (seleKind > 0) {
            tPoint.clone(tAbsorbPt);
        }

        return seleKind;
    }

    movePosOffset(tOffset, modCmdInfo) {
        if (this._selKind < 1) {
            return;
        }

        let seleKind = this.getSelKind();
        let position = this.getPos();
        let lenth = position.posList.length;
        if (seleKind == 1) {
            //整体移动
            if (this.symbolLeadwireFlag && this.m_pContainer) {
                // this.m_pContainer.translate(tOffset, modCmdInfo.mapscale, false);
                console.log("translate leadwire line");
            }
            else {
                this.translate(tOffset, modCmdInfo.mapscale, false);
            }
        }
        else if (seleKind < (lenth + ST_featurePT_IDX) && seleKind >= ST_featurePT_IDX) {
            //移动顶点
            if (this.symbolLeadwireFlag && this.m_pContainer) {
                this.translateLeadwireEndPoint(position, tOffset);
            }
            else {
                let selPosIdx = seleKind - ST_featurePT_IDX;
                this.translateSelVertext(selPosIdx, position, tOffset);
            }
        }
        else if (seleKind > (lenth + ST_featurePT_IDX - 1)) {
            let selMidPtInLineSecStIdx = seleKind - lenth - ST_featurePT_IDX;
            this.transLineSecPt(selMidPtInLineSecStIdx, position, tOffset);
        }
        let copyPosition = this.getPosCopy();
        let prePtCount = copyPosition.posList.length;
        let finalList = funcs.removeCollapsePoint(copyPosition.posList);
        if (prePtCount != finalList.length){
            this.setSelKind(0);
        }
        copyPosition.clear();
        finalList.forEach(pt => {
            copyPosition.addPoint(pt);
        });
        this.setPos(copyPosition);
        this.calScope(1);

    }


    transLineSecPt(selMidPtInLineSecStIdx, position, tOffset) {
        let list = position.posList;
        let lenth = list.length;
        let stPt = list[selMidPtInLineSecStIdx];
        let nextPt = null;
        if (selMidPtInLineSecStIdx < lenth - 1) {
            nextPt = list[selMidPtInLineSecStIdx + 1];
        }
        else {
            nextPt = list[0];
        }
        let hornVertKind = funcs.chkHornVertKind(stPt, nextPt);
        if (hornVertKind == 1) {
            stPt.y += tOffset.y;
            nextPt.y += tOffset.y;
        }
        else if (hornVertKind == 2) {
            stPt.x += tOffset.x;
            nextPt.x += tOffset.x;
        }
    }

    translateSelVertext(selPosIdx, position, tOffset) {
        //移动顶点
        let lenth = position.posList.length;
        let destPt = position.posList[selPosIdx];
        let preDestPt = { x: destPt.x, y: destPt.y };
        destPt.x += tOffset.x;
        destPt.y += tOffset.y;
        if (this.visionType == LGXEnums.lineVisionType.cross) {
            if (selPosIdx == 0 || selPosIdx == lenth - 1) {
                let newSelIdx = CrossModLineSvc.modifyEnd(this, preDestPt, destPt, selPosIdx);
                let newSelKind = ST_featurePT_IDX + newSelIdx;
                this.setSelKind(newSelKind);
            }
            else {
                this.modifyMultiPtLine_noneEnd(preDestPt, destPt, selPosIdx);
            }
        }

        if (this.m_topoRes) {
            let chgPtConnNode = null;
            if (selPosIdx == 0) {
                chgPtConnNode = this.m_topoRes.stNode;
            }
            else if (selPosIdx == lenth - 1) {
                chgPtConnNode = this.m_topoRes.endNode;
            }
            if (chgPtConnNode) {
                chgPtConnNode.setPos(destPt.x, destPt.y);
            }
        }
    }

    getMainFeaturePTLst(tFeaturePTLst,transformInfo) {
        var position = this.getPosCopy();
        if (this.chkClose()) {
            let p0 = position.posList[0];
            position.addPoint(p0);
        }

        var lenth = position.posList.length;
        for (var i = 0; i < lenth; i++) {
            var pt = position.posList[i];
            let featurePT = this.convertPt2World(pt,transformInfo);
            tFeaturePTLst.push({pt:featurePT,kind:LGXEnums.AuxPointKind.end});
        }

        let prePt = position.posList[0];
        for (let i = 1; i < lenth; i++) {
            let pt = position.posList[i];
            let x = (prePt.x + pt.x) / 2;
            let y = (prePt.y + pt.y) / 2;
            let t = { x, y };
            let featurePT = this.convertPt2World(t,transformInfo);
            tFeaturePTLst.push({pt:featurePT,kind:LGXEnums.AuxPointKind.lineMid});
            prePt = pt;
        }
    }

    getSubFeaturePTLst(tFeaturePTLst, mousePoint,transformInfo) {
        let position = this.getPosCopy();
        if (this.chkClose()) {
            let p0 = position.posList[0];
            position.addPoint(p0);
        }

        let lenth = position.posList.length;
        let tMayAbsorbPtLst = [];
        let prePt = position.posList[0];
        prePt = this.convertPt2World(prePt,transformInfo);
        for (let i = 1; i < lenth; i++) {
            let nextPt = position.posList[i];
            nextPt = this.convertPt2World(nextPt,transformInfo);
            let vector = funcs.getVector(prePt, nextPt);
            let projPt = funcs.getPointProj2Line(mousePoint, prePt, vector);
            if (projPt && funcs.chkPointOnLineSec(projPt, prePt, nextPt)) {
                let dis = funcs.distancePoint(projPt, mousePoint);
                let t = { dis, pt: projPt };
                tMayAbsorbPtLst.push(t);
            }
            prePt = nextPt;
        }

        //排序
        let sortedList = tMayAbsorbPtLst.sort(function (p1, p2) {
            if ((p1.dis - p2.dis) > 0) {
                return 1;
            }
            else {
                return -1;
            }
        }
        );
        if (sortedList.length) {
            let t = sortedList[0];
            tFeaturePTLst.push({ pt: t.pt, kind: LGXEnums.AuxPointKind.commonPt });
        }
    }

    assignInfo(info) {
        super.assignInfo(info);
        this.close = info.close;
        if (info.symbolLeadwireFlag == null || info.symbolLeadwireFlag == undefined) {
            // console.log("info.symbolLeadwireFlag ");
            this.symbolLeadwireFlag = false;
        }
        else {
            this.symbolLeadwireFlag = info.symbolLeadwireFlag;
        }
    }

    modifyAttribute(obj, mapscale) {
        super.modifyAttribute(obj,1);
        this.calScope(mapscale);
    }


    toInfo() {
        var info = new Polyline();
        this.toInfo4InheritClass(info);
        info.strPosition = this.getPos().toString();
        info.close = this.close;
        info.visionType = this.visionType;
        info.symbolLeadwireFlag = this.symbolLeadwireFlag;
        return info;
    }


    getPath() {
        let path = [];
        var position = this.getPos();
        var lenth = position.posList.length;
        for (var i = 0; i < lenth; i++) {
            var pt = position.posList[i];
            path.push({ x: pt.x, y: pt.y });
        }
        return path;
    }

    modifyCoord(propName, propVal, mapscale) {
        var position = this.getPos();
        if (propName == "poslist") {
            var lenth = position.posList.length;
            for (var i = 0; i < lenth; i++) {
                var pt = position.posList[i];
                let jsonObj = propVal[i];
                pt.x = parseFloat(jsonObj.x);
                pt.y = parseFloat(jsonObj.y);
            }
        }
        this.notifyToObserver();
        this.calScope(mapscale);
    }

    getCoordInfo() {
        let graphPropDef = this.getCoordPropDef();
        let tableData = [];
        let arr = [];
        var position = this.getPos();
        var lenth = position.posList.length;
        for (var i = 0; i < lenth; i++) {
            var pt = position.posList[i];
            arr.push({ x: pt.x, y: pt.y });
        }

        let polistObj = {
            propName: 'poslist',
            propLabel: '顶点列表',
            propValue: arr
        };
        tableData.push(polistObj);

        let tableForm = {
            'tableData': tableData
        };
        return { graphPropDef, tableForm };
    }

    getCoordPropDef() {
        let graphPropDef = {
            poslist: {
                vtype: global.PtList_CoordType,
                range: { min: 1, max: 10 },
            }
        }
        return graphPropDef;
    }

    getPosCopy() {
        let pos = new Coord.MultiPos();
        pos.clone(this._position);
        return pos;
    }

    getDimDatumFeatureLst() {
        let rstArr = [];

        let idkey = 1;
        //图形中心
        {
            let cnt = this.getZone().getCnt();
            let data = { x: cnt.x, y: cnt.y };
            let info = { type: LGXEnums.DimDatumType.center, data, id: idkey++ };
            rstArr.push(info);
        }

        let position = this.getPos();

        //顶点特征
        let lenth = position.posList.length;
        for (let i = 0; i < lenth; i++) {
            let pt = position.posList[i];
            let data = { idx: i, x: pt.x, y: pt.y }
            let info = { type: LGXEnums.DimDatumType.vertex, data, id: idkey++ };
            rstArr.push(info);
        }

        //线中心特征
        let prePt = position.posList[0];
        for (var i = 1; i < lenth; i++) {
            let cntPt = position.posList[i];
            let x = (prePt.x + cntPt.x) / 2;
            let y = (prePt.y + cntPt.y) / 2;
            let data = { stIdx: i - 1, endIdx: i, x, y };
            let info = { type: LGXEnums.DimDatumType.midpt, data, id: idkey++ };
            rstArr.push(info);
            prePt = cntPt;
        }

        //尾和头顶点
        if (this.close) {
            let cntPt = position.posList[0];
            let x = (prePt.x + cntPt.x) / 2;
            let y = (prePt.y + cntPt.y) / 2;
            let data = { stIdx: lenth - 1, endIdx: 0, x, y };
            let info = { type: LGXEnums.DimDatumType.midpt, data, id: idkey++ };
            rstArr.push(info);
        }

        //每条线段特征
        prePt = position.posList[0];
        for (var i = 1; i < lenth; i++) {
            let cntPt = position.posList[i];
            let st = { x: prePt.x, y: prePt.y };
            let end = { x: cntPt.x, y: cntPt.y };
            let data = { stIdx: i - 1, endIdx: i, st, end };
            let info = { type: LGXEnums.DimDatumType.edge, data, id: idkey++ };
            rstArr.push(info);
            prePt = cntPt;
        }

        //尾和头顶点
        if (this.close) {
            let cntPt = position.posList[0];
            let st = { x: prePt.x, y: prePt.y };
            let end = { x: cntPt.x, y: cntPt.y };
            let data = { stIdx: lenth - 1, endIdx: 0, st, end };
            let info = { type: LGXEnums.DimDatumType.edge, data, id: idkey++ };
            rstArr.push(info);
        }

        return rstArr;
    }


    //标注值发生变化的响应函数
    onDimDatumValueChgd(chgDatum,preDatumPt){
        console.log("rect graph onDimDatumValueChgd******");
        let svc = new PolylineDimChg(this);
        svc.onDimDatumValueChgd(chgDatum,preDatumPt);
    }

    onDimBaseChged(dimGraph) {
        console.log("polyline onDimBaseChged******");
        let svc = new PolylineDimBaseChange(this);
        svc.onBaseChanged(dimGraph);
    }

    setVisionType(v) {
        let position = this.getPos();
        let headPt = position.getHead();
        let tailPt = position.getTail();
        if (v != this.visionType) {
            if (v == "cross") {
                console.log("cross modify curent line");
                CrossModLineSvc.modifyEnd(this, headPt, headPt, 0);
            }
            else if (v == "straight") {
                position.clear();
                position.addPoint(headPt);
                position.addPoint(tailPt);
            }
        }
        this.visionType = v;
    }

    cloneSymbolInfo(symbol) {
        let unitList = symbol.getSymbolUnitLst();
        if (unitList.length < 1) {
            return;
        }
        let lineUnitInfo = unitList[0];
        console.log(lineUnitInfo);

        this._lineDef.color.setColorByValue(lineUnitInfo.color);
        this._lineDef.linemode = lineUnitInfo.lineMode;
        this._lineDef.joinmode = lineUnitInfo.lineJoin;
        this._lineDef.capmode = lineUnitInfo.lineCap;
        this._lineDef.lineWidth = lineUnitInfo.lineWidth;
        this._lineDef.stArrowType = lineUnitInfo.stArrowType;
        this._lineDef.endArrowType = lineUnitInfo.endArrowType;
    }

    chkAllSecHornVertical() {
        let position = this.getPosCopy();
        let p0 = position.posList[0];
        let lenth = position.posList.length;
        if (lenth < 2) {
            return false;
        }
        let flag = true;
        for (let i = 1; i < lenth; i++) {
            let p1 = position.posList[i];
            flag = funcs.chkAbsoluteHornVert(p0, p1);
            if (!flag) {
                break;
            }
            p0 = p1;
        }
        return flag;
    }

    translateLeadwireEndPoint(position, tOffset) {
        let lenth = position.posList.length;
        let destPt = position.posList[lenth - 1];
        let prePt = position.posList[lenth - 2];
        let vector = funcs.getVector(prePt, destPt);
        let transVector = new LGXCOM.LGXVector();
        transVector.setValue(tOffset.x, tOffset.y, 0);
        let transDis = Math.sqrt(tOffset.x * tOffset.x + tOffset.y * tOffset.y);
        let includeAngle = funcs.degree2Radian(vector.angleByCrossMultiply2Another(transVector));
        let preLenth = funcs.distancePoint(prePt, destPt, 1);
        let curentLenth = preLenth + transDis * Math.cos(includeAngle);

        destPt.x = prePt.x + curentLenth * vector.m;
        destPt.y = prePt.y + curentLenth * vector.n;
    }

    modifyMultiPtLine_noneEnd = function (mouseSelPt, curentPt, selPosIdx) {
        let position = this.getPos();
        let posList = position.posList;
        let ptCount = posList.length;

        let prePtIsLineHead = false;
        let prePt = posList[selPosIdx - 1];
        let isPrePtEnd = false;
        if (selPosIdx == 1) {
            isPrePtEnd = true;
            prePtIsLineHead = true;
        }
        let forwardPt = posList[selPosIdx + 1];
        let isForwardPtEnd = false;
        let fowardPtIsLineHead = true;
        if (selPosIdx == ptCount - 2) {
            isForwardPtEnd = true;
            fowardPtIsLineHead = false;
        }

        this.modifyPtNext2Curent(position, prePt, mouseSelPt, curentPt, isPrePtEnd, prePtIsLineHead);
        this.modifyPtNext2Curent(position, forwardPt, mouseSelPt, curentPt, isForwardPtEnd, fowardPtIsLineHead);
    }

    modifyPtNext2Curent = function (position, nextPoint, mouseSelPt, curentPt, isEndPt, isLineHead) {
        let vect1 = funcs.getVector(mouseSelPt, nextPoint);
        let angle1 = vect1.angleToAnotherVector_Degree(GlobalConstObj.xAaxisVector);
        if (Math.abs(angle1) < 45) {
            //水平处理
            if (!isEndPt) {
                nextPoint.y = curentPt.y;
            }
            else {
                let flag = this.modifyConnEndPt(position, nextPoint, mouseSelPt, curentPt, true, isLineHead);
                if (!flag) {
                    nextPoint.y = curentPt.y;
                }
            }
        }
        else {
            if (!isEndPt) {
                nextPoint.x = curentPt.x;
            }
            else {
                let flag = this.modifyConnEndPt(position, nextPoint, mouseSelPt, curentPt, false, isLineHead);
                if (!flag) {
                    nextPoint.x = curentPt.x;
                }
            }
        }
    }

    //正交联动修改末端有连接的线段
    modifyConnEndPt(position, endPoint, curentModifyOrginalPt, curentModifyPt, hornFlag, isLineHead) {
        let destNode = this.getModifyPtConnNode(curentModifyOrginalPt);
        if (!destNode) {
            return false;
        }

        let newPt1 = null;
        let newPt2 = null;
        if (hornFlag) {
            //在endPoint和curentModifyPt之间新增两个拐点,生成一段新的垂直线段
            let x = (endPoint.x + curentModifyPt.x) / 2;
            newPt1 = { x, y: endPoint.y };
            newPt2 = { x, y: curentModifyPt.y };
        }
        else {
            //在endPoint和curentModifyPt之间新增两个拐点,生成一段新的水平线段
            let y = (endPoint.y + curentModifyPt.y) / 2;
            newPt1 = { x: endPoint.x, y };
            newPt2 = { x: curentModifyPt.x, y };
        }
        let oldPosition = this.getPosCopy();
        let oldList = oldPosition.posList;
        position.clear();
        if (isLineHead) {
            position.addPoint(endPoint);
            position.addPoint(newPt1);
            position.addPoint(newPt2);
            for (let i = 1; i < oldList.length; i++) {
                let pt = oldList[i];
                position.addPoint(pt);
            }
            this.setSelKind(3 + ST_featurePT_IDX);
        }
        else {
            for (let i = 0; i < oldList.length - 1; i++) {
                let pt = oldList[i];
                position.addPoint(pt);
            }
            position.addPoint(newPt1);
            position.addPoint(newPt2);
            position.addPoint(endPoint);
            this.setSelKind(oldList.length - 2 + ST_featurePT_IDX);
        }
        return true;
    }

    getModifyPtConnNode(curentModifyOrginalPt) {
        if (!this.m_topoRes) {
            return null;
        }
        let destNode = null;
        let stNode = this.m_topoRes.stNode;
        let endNode = this.m_topoRes.endNode;
        if (stNode && endNode) {
            let p1 = stNode.getPos();
            let p2 = endNode.getPos();

            let dis1 = funcs.distancePoint(curentModifyOrginalPt, p1, 1);
            let dis2 = funcs.distancePoint(curentModifyOrginalPt, p2, 1);
            destNode = stNode;
            if (dis2 < dis1) {
                destNode = endNode;
            }
        }
        else if (stNode && !endNode) {
            let pt = stNode.getPos();
            let dis = funcs.distancePoint(curentModifyOrginalPt, pt, 1);
            if (dis < 5) {
                destNode = stNode;
            }
        }
        else if (!stNode && endNode) {
            let pt = endNode.getPos();
            let dis = funcs.distancePoint(curentModifyOrginalPt, pt, 1);
            if (dis < 5) {
                destNode = endNode;
            }
        }
        return destNode;
    }

    disBezierCurve(map, mapInfo) {
        var position = this.getPos();
        var ptCount = position.posList.length;
        if (ptCount < 2) {
            this.setUseful(0);
            return;
        }

        let pt0 = position.posList[0];
        let p0 = CoordTRFUtil.world2Screen(pt0, mapInfo);
        let pt1 = position.posList[ptCount - 1];
        let p3 = CoordTRFUtil.world2Screen(pt1, mapInfo);

        map.beginPath();
        map.moveTo(p0.x, p0.y);
        map.bezierCurveTo(p0.x, p0.y, p0.x, p3.y, p3.x, p3.y);
        Strokeutil.active(this._lineDef, this, map, mapInfo);
    }

    getLocalOrigin(){
        let pt = {x:0,y:0}
        let position = this.getPos();
        if (position.posList.length < 2){
            console.log();
        }
        let p0 = position.posList[0];
        pt.x = p0.x;
        pt.y = p0.y;
        return pt;
    }

    //复制粘贴时，从复制源对象复制属性到本图形对应属性----虚函数，由实体继承类实现
    copySubPropFromBuf(bufInfo,tx,ty){
        this.visionType  = bufInfo.visionType;
        this.close = bufInfo.close;
        this.symbolLeadwireFlag = bufInfo.symbolLeadwireFlag;
        this.copyCommnonPropFromBuf(bufInfo,tx,ty);
    }

    //检测point点是否落在当前图形对象的边界上
    chPtOnEdge(point,scaling){
        return funcs.chkPtOnPolyline(point,this._position.posList,global.SelectGap,scaling)
    }
}


export default { LineGraph }
